package com.powergisol.gis.listener;
import	java.util.ArrayList;
import	java.util.UUID;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.entity.*;
import com.powergisol.gis.service.SaveDataService;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.CnyZkExcelStandardVo;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.ExcelInportBaseVo;
import com.powergisol.gis.vo.TaskResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created By marc
 * Date: 2020/9/3  16:07
 * Description:
 */
public class LiangExcelModelListener extends AnalysisEventListener {

    private final static Pattern NUMBER_PATTERN = Pattern.compile("([+\\-]?[0-9]+[.]?[\\d]*)");

    String uuid;

    ExcelInportBaseVo baseVo;

    //组装数据存放
    TabBeam tabBeam;

    TabCoordinateSystem system;


    SaveDataService saveDataService;

    CacheManager cacheManager;

    String excelName;

    //字段值所在列数
    private Integer valueColumn;


    public LiangExcelModelListener() {
    }

    public LiangExcelModelListener(ExcelInportBaseVo baseVo,TabBeam tabBeam, SaveDataService saveDataService,
                                   CacheManager cacheManager, TabCoordinateSystem system, String excelName,String uuid) {
        this.baseVo = baseVo;
        this.tabBeam = tabBeam;
        this.saveDataService = saveDataService;
        this.cacheManager = cacheManager;
        this.system = system;
        this.excelName =  excelName;
        this.uuid = uuid;
    }
    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        ArrayList data = (ArrayList) o;
        try{
            if(data.isEmpty()){
                return;
            }
            int j = 0;
            int num = data.size();
            for(int i=0; i<num; i++){
                if(j>0){
                    return;
                }
                String str = String.valueOf(data.get(i));
                if(StringUtils.isBlank(str)) continue;
                //如果列数存在
                if(null == valueColumn && str.equals("字段值")){
                    valueColumn = i;
                }
                switch (str){
                    case "Hcdm01":
                        tabBeam.setHcdm01(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "Hcdm02":
                        tabBeam.setHcdm02(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "Hcdm03":
                        tabBeam.setHcdm03(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "Hcdm04":
                        tabBeam.setHcdm04(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "Hcdm05":
                        tabBeam.setHcdm05(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "Hcdm06":
                        tabBeam.setHcdm06(valueColumn >= num?null:data.get(valueColumn) == null ? null : data.get(valueColumn).toString());
                        j = j+1;
                        break;
                    case "KL21":
                        tabBeam.setKL21(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL22":
                        tabBeam.setKL22(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL23":
                        tabBeam.setKL23(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL24":
                        tabBeam.setKL24(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL25":
                        tabBeam.setKL25(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL26":
                        tabBeam.setKL26(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL28":
                        tabBeam.setKL28(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                    //
                    case "KL29":
                        tabBeam.setKL29(valueColumn >= num?null:data.get(valueColumn) == null ? null : Double.valueOf(data.get(valueColumn).toString()));
                        j = j+1;
                        break;
                   default :
                       break;
                }
            }

        }catch (Exception e) {
            dealFail("导入异常请检查数据是否正确");
            e.printStackTrace();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData(tabBeam);
    }
    private void saveData(TabBeam vo){
        try{
            List<ExcelInFailedDataVo> listFailed = new ArrayList<>();//失败数据
            BigDecimal lat = BigDecimal.ZERO;
            BigDecimal lon = BigDecimal.ZERO;
            BigDecimal lat1 = BigDecimal.ZERO;
            BigDecimal lon1 = BigDecimal.ZERO;
            BigDecimal x = BigDecimal.ZERO;
            BigDecimal y = BigDecimal.ZERO;
            BigDecimal x1 = BigDecimal.ZERO;
            BigDecimal y1 = BigDecimal.ZERO;

            if(1 == baseVo.getIsChange()){
                lat = XyChangeUtil.change(vo.getKL21(), vo.getKL22(),system.getCoorChangeLat());
                lon = XyChangeUtil.change(vo.getKL21(), vo.getKL22(),system.getCoorChangeLon());
                lat1 = XyChangeUtil.change(vo.getKL24(), vo.getKL25(),system.getCoorChangeLat());
                lon1 = XyChangeUtil.change(vo.getKL24(), vo.getKL25(),system.getCoorChangeLon());
            }else{
                lat = new BigDecimal(vo.getKL22());//纬度
                lon = new BigDecimal(vo.getKL21());//经度
                lat1 = new BigDecimal(vo.getKL25());//纬度
                lon1 = new BigDecimal(vo.getKL24());//经度
            }            //经度-180-180 纬度-90-90
            if (lon.compareTo(new BigDecimal("180")) > 0 || lon.compareTo(new BigDecimal("-180")) < 0 ||
                    lat.compareTo(new BigDecimal("90")) > 0 || lat.compareTo(new BigDecimal("-90")) < 0||
                    lon1.compareTo(new BigDecimal("180")) > 0 || lon1.compareTo(new BigDecimal("-180")) < 0 ||
                    lat1.compareTo(new BigDecimal("90")) > 0 || lat1.compareTo(new BigDecimal("-90")) < 0) {
                dealFail("坐标Xl或Y1或X2或Y2不在范围内");
                return;
            }
            x = new BigDecimal(vo.getKL21());
            y = new BigDecimal(vo.getKL22());
            x1 = new BigDecimal(vo.getKL24());
            y1 = new BigDecimal(vo.getKL25());

            tabBeam.setKL21(lon.doubleValue());
            tabBeam.setOKL21(x.doubleValue());

            tabBeam.setKL22(lat.doubleValue());
            tabBeam.setOKL22(y.doubleValue());

            tabBeam.setKL24(lon1.doubleValue());
            tabBeam.setOKL24(x1.doubleValue());

            tabBeam.setKL25(lat1.doubleValue());
            tabBeam.setOKL25(y1.doubleValue());

            List<TabBeam> list = new ArrayList<>();
            list.add(tabBeam);
            saveDataService.saveBatch(list);
        }catch (Exception e){
            e.printStackTrace();
            dealFail("解析失败");
        }

    }

    private void dealFail(String msg){
        List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
        ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
        failedDataVo.setReason(msg);
        failedDataVo.setRowIndex(excelName);
        listFailed.add(failedDataVo);
        Cache cache = cacheManager.getCache("cache-data");
        TaskResultVo resultVo = new TaskResultVo();
        resultVo.setListFailed(listFailed);
        cache.put(baseVo.getUuid() +"sb",resultVo);
    }

    private boolean isNumber(String value) {
        String matchStr = "^\\d+$";
        return value.matches(matchStr);
    }

}
